package com.zxct.wyserver.activity;

import android.app.AlertDialog;
import android.os.Bundle;
import android.support.v4.app.FragmentTabHost;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TabHost;

import com.zxct.wyserver.R;
import com.zxct.wyserver.base.BaseActivity;
import com.zxct.wyserver.fragment.HomeFragment;
import com.zxct.wyserver.fragment.MineFragment;
import com.zxct.wyserver.util.DialogUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends BaseActivity {

    @BindView(R.id.fth_main_tab)
    FragmentTabHost fthMainTab;
    private View tabBview;
    private TabHost.TabSpec tabSpec;

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {

    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {
        initWidgetBar();
    }

    @Override
    public void processClick(View view) {

    }

    /**
     * 加载底部导航栏
     * altered by lanyu
     */
    private void initWidgetBar() {
        fthMainTab.setup(this, getSupportFragmentManager(), R.id.fl_main_fragment);
        setTabIndicator("HomeFragment", R.layout.tab_home, HomeFragment.class);
        setTabIndicator("MineFragment", R.layout.tab_mine, MineFragment.class);
    }

    private void setTabIndicator(String string, int layout, Class clazz) {
        tabSpec = fthMainTab.newTabSpec(string);
        tabBview = View.inflate(this, layout, null);
        tabSpec.setIndicator(tabBview);
        fthMainTab.addTab(tabSpec, clazz, null);
    }

    //监听返回键，按返回提示
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            DialogUtils.showDialog(context,"确定退出登录吗？","确定","取消");
            new DialogUtils().setOnButtonClickListener(new DialogUtils.OnButtonClickListener() {
                @Override
                public void onPositiveButtonClick(AlertDialog dialog) {
                    finish();
                    System.exit(0);//正常退出APP
                }
                @Override
                public void onNegativeButtonClick(AlertDialog dialog) {
                    dialog.dismiss();
                }
            });
        }
        return false;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
